<!DOCTYPE html>
<html lang="zh-CN">





<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/blog/img/apple-touch-icon.png">
  <link rel="icon" type="image/png" href="/blog/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="兴趣使然的个人博客">
  <meta name="author" content="Ilirus">
  <meta name="keywords" content>
  <title>Java8中新增的时间处理API - Ilirus</title>

  <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css">


  <link rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css">
  <link rel="stylesheet" href="/blog/lib/hint/hint.min.css">

  
    <link rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/atom-one-light.min.css">
  

  


<!-- 主题依赖的图标库，不要自行修改 -->
<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_yg9cfy8wd6.css">

<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_pjno9b9zyxs.css">

<link rel="stylesheet" href="/blog/css/main.css">

<!-- 自定义样式保持在最底部 -->


  <script src="/blog/js/utils.js"></script>
</head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/blog/">&nbsp;<strong>Ilirus</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="view intro-2" id="background" parallax=true
         style="background: url('/blog/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container text-center white-text fadeInUp">
            <span class="h2" id="subtitle">
              
            </span>

            
              
  <div class="mt-3 post-meta">
    <i class="iconfont icon-date-fill" aria-hidden="true"></i>
    <time datetime="2020-04-18 23:40">
      2020年4月18日 晚上
    </time>
  </div>


<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1.4k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      20
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <div class="post-content mx-auto" id="post">
            
            <article class="markdown-body">
              <h1 id="Java8新的时间处理API"><a href="#Java8新的时间处理API" class="headerlink" title="Java8新的时间处理API"></a>Java8新的时间处理API</h1><p><strong>原有的<code>java.util.Date</code>与新增的<code>java.time</code>有何差异？</strong></p>
<ul>
<li><code>java.util.Date</code>设定为可变类型</li>
<li><code>SimpleDateFormat</code>是非线程安全的</li>
<li>新的API 基于ISO 标准日历系统, 遵循Gregorian 规则</li>
<li><code>java.time</code>包下的所有类都是不可变类型</li>
<li><code>java.time</code>包下的所有类都是线程安全的</li>
</ul>
<h2 id="java-time简单介绍"><a href="#java-time简单介绍" class="headerlink" title="java.time简单介绍"></a><code>java.time</code>简单介绍</h2><p><code>java.time</code>由以下5个包组成:</p>
<ul>
<li><code>java.time</code>: 包含值对象的基础包</li>
<li><code>java.time.chrono</code>: 提供对不同的日历系统的访问</li>
<li><code>java.time.format</code>: 格式化和解析时间和日期</li>
<li><code>java.time.temporal</code>: 包括底层框架和扩展特性</li>
<li><code>java.time.zone</code>: 包含时区支持的类</li>
</ul>
<p>关于<code>java.time</code>下, 类的简介:</p>
<ul>
<li><code>Instant</code>: 时间戳</li>
<li><code>LocalDate</code>: 只包含日期，比如: 2020-01-01</li>
<li><code>LocalTime</code>: 只包含时间，比如: 12:00:00</li>
<li><code>LocalDateTime</code>: 包含日期和时间，比如: 2020-01-01T12:00:00</li>
<li><code>Duration</code>: 时间段, 主要用于计算两个时间之间的差值</li>
<li><code>Peroid</code>: 时间段, 主要用于计算两个日期之间的差值</li>
<li><code>ZoneOffset</code>: 时区偏移量，比如: +8:00</li>
<li><code>ZonedDateTime</code>: 带时区的日期时间</li>
<li><code>Clock</code>: 时钟，可以获取或指定时区, 也可获取当前的时刻</li>
<li><code>DateTimeFormatter</code>: 时间格式化</li>
</ul>
<p>关于<code>java.time</code>下<strong>大部分</strong>类的方法<strong>前缀</strong>简介:</p>
<ul>
<li><code>of</code>: 静态工厂方法(用类名去调用)。</li>
<li><code>parse</code>: 静态工厂方法，关注于解析(用类名去调用)。</li>
<li><code>now</code>: 静态工厂方法，用当前时间创建实例(用类名去调用)</li>
<li><code>get</code>: 获取某些东西的值。</li>
<li><code>is</code>: 检查某些东西的是否是true。</li>
<li><code>with</code>: 返回一个部分状态改变了的时间日期对象拷贝(单独一个with方法,参数为TemporalAdjusters类型)</li>
<li><code>plus</code>: 返回一个时间增加了的、时间日期对象拷贝(如果参数是负数也能够有minus方法的效果)</li>
<li><code>minus</code>: 返回一个时间减少了的、时间日期对象拷贝</li>
<li><code>to</code>: 把当前时间日期对象转换成另外一个，可能会损失部分状态.</li>
<li><code>at</code>: 把这个对象与另一个对象组合起来，例如: date.atTime(time)。</li>
<li><code>format</code>: 根据某一个DateTimeFormatter格式化为字符串</li>
</ul>
<a id="more"></a>
<h2 id="新的API要如何使用"><a href="#新的API要如何使用" class="headerlink" title="新的API要如何使用"></a>新的API要如何使用</h2><h3 id="获取当前的时间日期"><a href="#获取当前的时间日期" class="headerlink" title="获取当前的时间日期"></a>获取当前的时间日期</h3><pre><code class="hljs java">LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
<span class="hljs-comment">// 使用.withNano(0) 来去除纳秒数显示</span>
LocalDateTime datetime = LocalDateTime.now().withNano(<span class="hljs-number">0</span>);

System.out.println(date);  <span class="hljs-comment">// 2020-04-18</span>
System.out.println(time);  <span class="hljs-comment">// 20:41:02.626861700</span>
System.out.println(datetime);  <span class="hljs-comment">// 2020-04-18T20:41:02</span></code></pre>
<h3 id="与字符串相互转换"><a href="#与字符串相互转换" class="headerlink" title="与字符串相互转换"></a>与字符串相互转换</h3><pre><code class="hljs java"><span class="hljs-keyword">final</span> LocalDateTime now = LocalDateTime.now().withNano(<span class="hljs-number">0</span>);

<span class="hljs-keyword">final</span> DateTimeFormatter patternToString = DateTimeFormatter.ofPattern(<span class="hljs-string">"G yyyy年MM月dd号 E a hh时mm分ss秒"</span>);
<span class="hljs-keyword">final</span> String byFormat = now.format(patternToString);
System.out.println(byFormat);  <span class="hljs-comment">// 公元 2020年04月18号 周六 下午 02时11分43秒</span>

<span class="hljs-keyword">final</span> DateTimeFormatter patternToDate = DateTimeFormatter.ofPattern(<span class="hljs-string">"yyyy-MM-dd HH:mm:ss"</span>);
<span class="hljs-keyword">final</span> LocalDateTime byParse = LocalDateTime.parse(<span class="hljs-string">"2020-04-18 14:11:43"</span>, patternToDate);
System.out.println(byParse);  <span class="hljs-comment">// 2020-04-18T14:11:43</span></code></pre>
<h3 id="获取单独的年月日时分秒信息"><a href="#获取单独的年月日时分秒信息" class="headerlink" title="获取单独的年月日时分秒信息"></a>获取单独的年月日时分秒信息</h3><pre><code class="hljs java">LocalDate date = LocalDate.now();
<span class="hljs-keyword">int</span> year = date.getYear();
<span class="hljs-keyword">int</span> month = date.getMonthValue();
<span class="hljs-keyword">int</span> day = date.getDayOfMonth();
System.out.println(year);   <span class="hljs-comment">// 2020</span>
System.out.println(month);  <span class="hljs-comment">// 4</span>
System.out.println(day);    <span class="hljs-comment">// 18</span>

LocalTime time = LocalTime.now();
<span class="hljs-keyword">int</span> hour = time.getHour();
<span class="hljs-keyword">int</span> minute = time.getMinute();
<span class="hljs-keyword">int</span> second = time.getSecond();
<span class="hljs-keyword">int</span> nano = time.getNano();
System.out.println(hour);    <span class="hljs-comment">// 8(时)</span>
System.out.println(minute);  <span class="hljs-comment">// 2(分)</span>
System.out.println(second);  <span class="hljs-comment">// 5(秒)</span>
System.out.println(nano);    <span class="hljs-comment">// 453345500(纳秒)</span>

LocalDateTime dateTime = LocalDateTime.now();
System.out.println(dateTime.getYear());  <span class="hljs-comment">// 2020</span>
System.out.println(dateTime.getHour());  <span class="hljs-comment">// 8</span></code></pre>
<h3 id="自定义日期时间"><a href="#自定义日期时间" class="headerlink" title="自定义日期时间"></a>自定义日期时间</h3><pre><code class="hljs java"><span class="hljs-keyword">final</span> LocalDate date = LocalDate.of(<span class="hljs-number">2020</span>, <span class="hljs-number">4</span>, <span class="hljs-number">18</span>);
<span class="hljs-keyword">final</span> LocalTime time = LocalTime.of(<span class="hljs-number">8</span>, <span class="hljs-number">20</span>, <span class="hljs-number">45</span>);
<span class="hljs-keyword">final</span> LocalDateTime dateTime = LocalDateTime.of(date, time);
<span class="hljs-comment">// 使用LocalDateTime.of(2020,4,18,8,10,30)方式也可创建自定义LocalDateTime</span>
<span class="hljs-comment">// 因为没有指定nano值, 所以不会出现出现纳秒显示</span>
System.out.println(dateTime);  <span class="hljs-comment">// 2020-04-18T08:20:45</span></code></pre>
<h3 id="时间比较"><a href="#时间比较" class="headerlink" title="时间比较"></a>时间比较</h3><pre><code class="hljs java"><span class="hljs-keyword">final</span> LocalDate date1 = LocalDate.now();  <span class="hljs-comment">// 假设今天为2020-4-18</span>
<span class="hljs-keyword">final</span> LocalDate date2 = LocalDate.of(<span class="hljs-number">2020</span>, <span class="hljs-number">4</span>, <span class="hljs-number">18</span>);
<span class="hljs-comment">// LocalTime和LocalDateTime 同理</span>
<span class="hljs-comment">// equals: 比较是否相等</span>
<span class="hljs-keyword">if</span>(date1.equals(date2)) &#123;
    System.out.println(<span class="hljs-string">"相等"</span>);
&#125; <span class="hljs-keyword">else</span> &#123;
    System.out.println(<span class="hljs-string">"不相等"</span>);
&#125;

<span class="hljs-comment">// isAfter: 是否晚于某个日期</span>
<span class="hljs-comment">// isBefore: 是否早于某个日期</span>
<span class="hljs-keyword">if</span>(date1.isAfter(date2)) &#123;
    System.out.println(date1+<span class="hljs-string">" 晚于 "</span>+date2);
&#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(date1.isBefore(date2)) &#123;
    System.out.println(date1+<span class="hljs-string">" 早于 "</span>+date2);
&#125;</code></pre>
<h3 id="检查周期性事件"><a href="#检查周期性事件" class="headerlink" title="检查周期性事件"></a>检查周期性事件</h3><pre><code class="hljs java"><span class="hljs-keyword">final</span> LocalDate now = LocalDate.now();

<span class="hljs-keyword">final</span> MonthDay targetMonthDay = MonthDay.of(<span class="hljs-number">10</span>, <span class="hljs-number">1</span>);
<span class="hljs-keyword">final</span> MonthDay currentMonthDay = MonthDay.from(now);

<span class="hljs-keyword">if</span>(currentMonthDay.equals(targetMonthDay)) &#123;
    System.out.println(<span class="hljs-string">"国庆到了"</span>);
&#125;</code></pre>
<h3 id="计算某段时间之前或之后的日期"><a href="#计算某段时间之前或之后的日期" class="headerlink" title="计算某段时间之前或之后的日期"></a>计算某段时间之前或之后的日期</h3><pre><code class="hljs java"><span class="hljs-keyword">final</span> LocalDateTime dateTime = LocalDateTime.now().withNano(<span class="hljs-number">0</span>);
<span class="hljs-keyword">final</span> LocalDateTime dateTime2 = dateTime
        <span class="hljs-comment">// 计算当前时间的 一个月后</span>
        .plusMonths(<span class="hljs-number">1</span>)
        <span class="hljs-comment">// 又 一周后</span>
        .plusWeeks(<span class="hljs-number">1</span>)
        <span class="hljs-comment">// 又 2个半天(12h)后</span>
        .plus(<span class="hljs-number">2</span>, ChronoUnit.HALF_DAYS)
        <span class="hljs-comment">// 又 1小时前</span>
        .minusHours(<span class="hljs-number">1</span>)
        <span class="hljs-comment">// 又 2个月前 的时间</span>
        .minus(<span class="hljs-number">2</span>, ChronoUnit.MONTHS);
System.out.println(dateTime2);</code></pre>
<h3 id="Clock-时钟类的用法"><a href="#Clock-时钟类的用法" class="headerlink" title="Clock 时钟类的用法"></a>Clock 时钟类的用法</h3><pre><code class="hljs java"><span class="hljs-keyword">final</span> Clock clock = Clock.systemUTC();
<span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> millis = clock.millis();
System.out.println(millis);  <span class="hljs-comment">// 1587320754163</span>

System.out.println(millis == System.currentTimeMillis());  <span class="hljs-comment">// true</span>
<span class="hljs-comment">// Instant.now().toEpochMilli() 也可获取时间戳</span>
System.out.println(millis == Instant.now().toEpochMilli());  <span class="hljs-comment">// true</span>

<span class="hljs-keyword">final</span> Clock zone = Clock.systemDefaultZone();
ZoneId id = zone.getZone();
System.out.println(id);  <span class="hljs-comment">// Asia/Shanghai</span>

System.out.println(id.getId().equals(TimeZone.getDefault().getID()));  <span class="hljs-comment">// true</span>

<span class="hljs-keyword">final</span> LocalDateTime now = LocalDateTime.now().withNano(<span class="hljs-number">0</span>);
<span class="hljs-keyword">final</span> ZoneId zoneIdOfHonolulu = ZoneId.of(<span class="hljs-string">"Pacific/Honolulu"</span>);
<span class="hljs-keyword">final</span> ZonedDateTime timeInHonolulu = ZonedDateTime.of(now, zoneIdOfHonolulu);
System.out.println(timeInHonolulu);  <span class="hljs-comment">// 2020-04-20T02:25:24-10:00[Pacific/Honolulu]</span></code></pre>
<h3 id="其他用法"><a href="#其他用法" class="headerlink" title="其他用法"></a>其他用法</h3><pre><code class="hljs java"><span class="hljs-keyword">final</span> LocalDateTime now = LocalDateTime.now().withNano(<span class="hljs-number">0</span>);
<span class="hljs-comment">// 判断是否是闰年, LocalDate类才有此方法</span>
<span class="hljs-keyword">if</span>(LocalDate.from(now).isLeapYear()) &#123;
    System.out.println(<span class="hljs-string">"今年是闰年"</span>);
&#125;
<span class="hljs-comment">// 获取星期</span>
System.out.println(now.getDayOfWeek());  <span class="hljs-comment">// SATURDAY</span>

<span class="hljs-keyword">final</span> LocalTime time = LocalTime.of(<span class="hljs-number">12</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>);
<span class="hljs-keyword">final</span> LocalDate start = LocalDate.of(<span class="hljs-number">2018</span>, <span class="hljs-number">8</span>, <span class="hljs-number">18</span>);
<span class="hljs-keyword">final</span> LocalDate end = LocalDate.of(<span class="hljs-number">2020</span>, <span class="hljs-number">8</span>, <span class="hljs-number">18</span>);
<span class="hljs-keyword">final</span> LocalDateTime startDate = LocalDateTime.of(start, time);
<span class="hljs-keyword">final</span> LocalDateTime endDate = LocalDateTime.of(end, time);

<span class="hljs-comment">// 计算两个日期直接的差值, 如果第一个参数的时间晚于第二个参数, 则该类的其他方法结果会返回负数</span>
<span class="hljs-keyword">final</span> Duration between = Duration.between(startDate, endDate);
<span class="hljs-comment">// 计算天数差值</span>
System.out.println(between.toDays());     <span class="hljs-comment">// 731 (第一个参数晚于第二个参数则会返回负数-731)</span>
<span class="hljs-comment">// 计算小时数差值</span>
System.out.println(between.toHours());    <span class="hljs-comment">// 17544   (-17544)</span>
<span class="hljs-comment">// 计算分钟数差值</span>
System.out.println(between.toMinutes());  <span class="hljs-comment">// 1052640 (-1052640)</span>

<span class="hljs-keyword">final</span> Period period = Period.between(LocalDate.from(startDate),LocalDate.from(endDate));
<span class="hljs-comment">// 计算月份差值</span>
System.out.println(period.toTotalMonths()); <span class="hljs-comment">// 24</span></code></pre>

            </article>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/blog/categories/Java/">Java</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/blog/tags/Java/">Java</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a href="https://zh.wikipedia.org/wiki/Wikipedia:CC_BY-SA_3.0%E5%8D%8F%E8%AE%AE%E6%96%87%E6%9C%AC" rel="nofollow noopener">CC BY-SA 3.0协议</a> 。转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <div class="post-prev col-6">
                    
                    
                      <a href="/blog/2020/04/20/Java是值传递还是引用传递/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Java是值传递还是引用传递</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </div>
                  <div class="post-next col-6">
                    
                    
                      <a href="/blog/2020/04/11/OAuth2-0资源服务器搭建与配置/">
                        <span class="hidden-mobile">OAuth2.0资源服务器搭建与配置</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </div>
                </div>
              
            </div>

            
              <!-- Comments -->
              <div class="comments" id="comments">
                
                
  <div id="lv-container" data-id="city" data-uid="MTAyMC80NTMxNi8yMTgyOQ==">
    <script type="text/javascript">
      function loadLivere() {
        addScript('https://cdn-city.livere.com/js/embed.dist.js');
      }
      createObserver(loadLivere, 'lv-container');
    </script>
    <noscript>为正常使用来必力评论功能请允许 JavaScript 运行</noscript>
  </div>


              </div>
            
          </div>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  

  <footer class="mt-5">
  <div class="text-center py-3">
    <div>
      <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a>
      <i class="iconfont icon-love"></i>
      <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener">
        <span>Fluid</span></a>
    </div>
    

    

    
  </div>
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/blog/js/debouncer.js" ></script>
<script  src="/blog/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/blog/js/lazyload.js" ></script>
  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/blog/js/clipboard-use.js" ></script>







  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: 'article.markdown-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "Java8中新增的时间处理API&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/blog/js/local-search.js" ></script>
  <script>
    var path = "/blog/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>







  
  
    <script type="text/javascript">
      //定义获取词语下标
      var a_idx = 0;
      jQuery(document).ready(function ($) {
        //点击body时触发事件
        $("body").click(function (e) {
          //需要显示的词语
          var a = new Array("富强", "民主", "文明", "和谐", "自由", "平等", "公正", "法治", "爱国", "敬业", "诚信", "友善");
          //设置词语给span标签
          var $i = $("<span/>").text(a[a_idx]);
          //下标等于原来下标+1  余 词语总数
          a_idx = (a_idx + 1) % a.length;
          //获取鼠标指针的位置，分别相对于文档的左和右边缘。
          //获取x和y的指针坐标
          var x = e.pageX, y = e.pageY;
          //在鼠标的指针的位置给$i定义的span标签添加css样式
          $i.css({
            "z-index": 999,
            "top": y - 20,
            "left": x,
            "position": "absolute",
            "font-weight": "bold",
            "color": rand_color()
          });
          // 随机颜色
          function rand_color() {
            return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")"
          }
          //在body添加这个标签
          $("body").append($i);
          //animate() 方法执行 CSS 属性集的自定义动画。
          //该方法通过CSS样式将元素从一个状态改变为另一个状态。CSS属性值是逐渐改变的，这样就可以创建动画效果。
          //详情请看http://www.w3school.com.cn/jquery/effect_animate.asp
          $i.animate({
            //将原来的位置向上移动180
            "top": y - 180,
            "opacity": 0
            //1500动画的速度
          }, 1500, function () {
            //时间到了自动删除
            $i.remove();
          });
        });
      })
      ;
    </script>
  














<script src="/blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"/blog/live2dw/assets/tororo.model.json"},"display":{"position":"left","width":300,"height":300},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
